function getAccountFromIdentityName(name) {
	var sAccountManager = getAccountManager();
	var accounts = sAccountManager.accounts;
	if (accounts.Count() != 0) {
		for(var i = 0 ; i < accounts.Count() ; i++) {
			var account = accounts.QueryElementAt(i, Components.interfaces.nsIMsgAccount);
			var identity = getAccountIdentity(account);
			if ((identity != null) && (identity.identityName == name) ){
				return account;
			}
		}
	}
	return null;
}

function dumpAccountNames() {
	var sAccountManager = getAccountManager();
	var accounts = sAccountManager.accounts;
	if (accounts.Count() != 0) {
		for(var i = 0 ; i < accounts.Count() ; i++) {
			var account = accounts.QueryElementAt(i, Components.interfaces.nsIMsgAccount);
			var identity = getAccountIdentity(account);
			if (identity != null) {
				log(identity.identityName);
			} else {
				log("An identity is null !");
			}
		}
	}
}

function getAccountIdentity(account) {
	var identities = account.identities;
	if (identities.Count() != 0) {
		return identities.QueryElementAt(0, Components.interfaces.nsIMsgIdentity);
	}
	return null;
}

function getAccountFolder(account) {
	var accountIncomingServer = account.incomingServer;
	return accountIncomingServer.rootMsgFolder;
}

function getSubfolderFromName(folder, subfolderName) {
	var subfolders = folder.subFolders;
	while(subfolders.hasMoreElements()) {
		var sub = subfolders.getNext();
		if(sub.prettiestName == subfolderName) {
			return sub;
		}
	}
	return null;
}

function newCopyServiceListener() {
	var copyServiceListener = {
			QueryInterface : function(iid) {
				if (iid.equals(Components.interfaces.nsIMsgCopyServiceListener) || iid.equals(Components.interfaces.nsISupports))
					return this;
				throw Components.results.NS_NOINTERFACE;
				return 0;
			},
		OnProgress: function (progress, progressMax) {},
		OnStartCopy: function () {},
		OnStopCopy: function (status) {},
		SetMessageKey: function (key) { this.key=key; }
	}
	return copyServiceListener;
}

function getHeader(message) {
	return message.QueryInterface(Components.interfaces.nsIMsgDBHdr);
}

function getAuthor(message) {
	return getHeader(message).author;
}

function getSubject(message) {
	return getHeader(message).subject;
}

function copyMessage(sourceFolder, message, destinationfolder) {
	copyOrMoveMessage(sourceFolder, createMutableArrayAndAdd(message), destinationfolder, false);
}

function copyMessages(sourceFolder, messages, destinationfolder) {
	copyOrMoveMessage(sourceFolder, messages, destinationfolder, false);
}

function moveMessage(sourceFolder, message, destinationfolder) {
	copyOrMoveMessage(sourceFolder, createMutableArrayAndAdd(message), destinationfolder, true);
}

function moveMessages(sourceFolder, messages, destinationfolder) {
	copyOrMoveMessage(sourceFolder, messages, destinationfolder, true);
}

function createMutableArrayAndAdd(object) {
	var messageArray = createMutableArray();
	messageArray.appendElement(object, false);
	return messageArray;
}

function copyOrMoveMessage(sourceFolder, messagesArray, destinationfolder, copyOrMove) {
	if (traceEnabled) {
		var enum = messagesArray.enumerate();
		while(enum.hasMoreElements()) {
			var message = enum.getNext();
			log("The mail from [" + getAuthor(message) + "] with subject [" + getSubject(message) + "] will be moved to [" + destinationfolder.prettiestName + "]");
		}
	}	
	var copyService = createCopyService();
	copyService.CopyMessages(sourceFolder, messagesArray, destinationfolder, copyOrMove, newCopyServiceListener(), null, false);
}